<!--车辆维修管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">录入维修车辆</el-button>
  </div>
  <!-- 录入维修车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input disabled :value="user.username"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择车牌号" v-model="saveRepairForm.vehicleId" :disabled="saveRepairForm.id!=null">
              <el-option v-for="item in vehicleList"  :label="item.license" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveRepairForm.item">
              <el-option v-for="item in itemOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveRepairForm.addr">
              <el-option v-for="item in addrOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料" >
            <el-select placeholder="请选择" v-model="saveRepairForm.material">
              <el-option v-for="item in materialOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用" >
            <el-input placeholder="请输入" v-model="saveRepairForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="saveRepairForm.repairTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
              <el-radio-group v-model="saveRepairForm.status">
                <el-radio value="1" border style="margin:0;">维修中</el-radio>
                <el-radio value="0" border>已完成</el-radio>
              </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepair">录入</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆" style="width:290px;">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="searchRepairForm.vehicleLicense"></el-input>
      </el-form-item>
      <el-form-item label="送修人员">
        <el-input placeholder="请输入送修人姓名" style="width:220px;" v-model="searchRepairForm.username"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width: 290px;">
        <el-select placeholder="请选择维修状态" v-model="searchRepairForm.status">
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepair">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="repairArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="vehicleLicense" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="itemFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="addrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="statusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRepair(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const user=ref(getUser());

//控制录入维修车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入维修车辆');

const repairArr = ref([
    {
        license: '京A22323',
        item: '汽车保养',
        username: 'mike',
        addr: '沃尔沃专修店',
        repairTime: '2021-01-01',
        fee: '10034.45',
        material: '汽缸垫进排气歧管垫',
        status: '维修中'
    },
    {
        license: '京B22323',
        item: '汽车美容',
        username: 'lucy',
        addr: '美奥行汽修',
        repairTime: '2024-11-23',
        fee: '613.27',
        material: '气门弹簧',
        status: '维修中'
    },
  {
    license: '京C22323',
    item: '钣金喷漆',
    username: 'tom',
    addr: '悦心金峰盛车检汽修',
    repairTime: '2022-12-21',
    fee: '2653.5',
    material: '板簧胶套',
    status: '已完成'
  }
]);
const saveRepairForm = ref({
  material: '',
  repairTime: '',
  item: '',
  fee: '',
  addr: '',
  vehicleId: ''
});
const materialOptions=ref([]);
//维修材料显示转换
const materialFormatter = (row,column,cellValue,index)=>{
  if(!materialOptions.value){
    return cellValue;
  }
  for(let item of materialOptions.value){
    if(item.value==cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const itemOptions=ref([]);
//维修项目显示转换
const itemFormatter = (row,column,cellValue,index)=>{
  if(!itemOptions.value){
    return cellValue;
  }
  for(let item of itemOptions.value){
    if(item.value==cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const addrOptions=ref([]);
//维修地址显示转换
const addrFormatter = (row,column,cellValue,index)=>{
  if(!addrOptions.value){
    return cellValue;
  }
  for(let item of addrOptions.value){
    if(item.value==cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

const vehicleList=ref([]);
const loadVehicle = ()=>{
  axios.get(BASE_URL+'/v1/vehicle/select?status=1').then((response)=>{
    if (response.data.code==2000){
      vehicleList.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
const saveRepair = ()=>{
  saveRepairForm.value.userId=user.value.id;
  saveRepairForm.value.username=user.value.username;
  let data=qs.stringify(saveRepairForm.value)
  axios.post(BASE_URL+'/v1/repair/save',data).then(res=>{
    if(res.data.code==2000){
      dialogVisible.value=false;
      ElMessage.success('录入成功');
      saveRepairForm.value={};
      loadRepair();
    }
  })
}
//实现关闭功能
const handleClose = ()=>{
  if (confirm('确认关闭本窗口?')){
    dialogVisible.value=false;
    saveRepairForm.value={};
  }
}
const searchRepairForm=ref({
  vehicleLicense: '',
  username: '',
  status: '',
});
const statusFormatter=(row,column,cellValue,index)=>{
  if(cellValue==1){
    cellValue='维修中';
  }else{
    cellValue='已完成';
  }
  return cellValue;
}
const loadRepair=()=>{
  let data = qs.stringify(searchRepairForm.value);
  axios.get(BASE_URL+'/v1/repair/select?'+data).then(res=>{
    if(res.data.code==2000){
      repairArr.value=res.data.data;
    }
  })
  loadVehicle();
};

const resetSearch=()=>{
  //清空已填的搜索数据,并且重新加载所有车辆信息
  searchRepairForm.value = {};
  loadRepair();
}


const editRepair=(id)=>{
  axios.get(BASE_URL+'/v1/repair/select?id='+id).then(res=>{
    if(res.data.code==2000){
      saveRepairForm.value=res.data.data[0];
      dialogVisible.value=true;
      dialogTitle.value='编辑维修车辆';
    }
  })

}

const loadDictOptions = (object,dictCode)=>{
  //向后端发请求获取字典项
  axios.get(BASE_URL+'/v1/dictoption/select/'+dictCode).then((response)=>{
    if(response.data.code==2000){
      object.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  });
}

onMounted(()=>{
  loadRepair();
  loadDictOptions(materialOptions,'repair_material');
  loadDictOptions(itemOptions,'repair_item');
  loadDictOptions(addrOptions,'repair_addr');
});

</script>